package com.bootdo.system.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.ListDao;
import com.bootdo.system.dao.ReportImgDao;
import com.bootdo.system.domain.ListDO;
import com.bootdo.system.domain.ReportImgDO;
import com.bootdo.system.service.ListService;
import com.bootdo.system.vo.DeliveryDetailVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service
public class ListServiceImpl implements ListService {

    private static final Logger logger = LoggerFactory.getLogger(ListServiceImpl.class);
    @Autowired
    private ListDao listDao;

    @Autowired
    private ReportImgDao reportImgDao;

    @Override
    public ListDO get(String id) {
        return listDao.get(id);
    }

    @Override
    public List<DeliveryDetailVO> list(Map<String, Object> map) {
        List<DeliveryDetailVO> deliveryDetailVOS = listDao.list(map);
        for(DeliveryDetailVO deliveryDetailVO : deliveryDetailVOS){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            if(deliveryDetailVO.getFreightDate() != null){
                deliveryDetailVO.setFreightTime(format.format(deliveryDetailVO.getFreightDate()));
            }
            //根据产品名称和批号查询质检报告图片
            List<ReportImgDO> reportImgDOS = new ArrayList<>();
            reportImgDOS = reportImgDao.getImgInfo(deliveryDetailVO.getProductName(),
                    deliveryDetailVO.getBatchNumber(), "", deliveryDetailVO.getProductGuige(), deliveryDetailVO.getValidDate());
            if(reportImgDOS.isEmpty()){
                reportImgDOS = reportImgDao.getImgInfo(deliveryDetailVO.getProductName(),
                        deliveryDetailVO.getBatchNumber(), "", "", "");
            }
            if(!reportImgDOS.isEmpty()){
                deliveryDetailVO.setImgUrls(reportImgDOS);
            }
        }
//        List<DeliveryDetailVO> list = deliveryDetailVOS.stream().filter(s -> s.getImgUrls() != null).collect(Collectors.toList());
//        System.out.println(list);
        return deliveryDetailVOS;
    }

    @Override
    public int count(Map<String, Object> map) {
        return listDao.count(map);
    }

    @Override
    public int save(ListDO list) {
        return listDao.save(list);
    }

    @Override
    public int update(ListDO list) {
        return listDao.update(list);
    }

    @Override
    public int remove(String id) {
        return listDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return listDao.batchRemove(ids);
    }

    @Override
    public void importInfo(MultipartFile file, Long userId) throws Exception {
        try {
            // 导入模板参数
            ImportParams importParams = new ImportParams();
            // easypoi 导入数据
            ExcelImportResult<ListDO> result =
                    ExcelImportUtil.importExcelMore(file.getInputStream(), ListDO.class, importParams);
            // 获取物料数据
            List<ListDO> list = result.getList();
            if (CollectionUtils.isNotEmpty(list)) {
                //查询当天的数据导入次数
                Integer importNum = listDao.getImportNum();
                if(null == importNum){
                    importNum = 1;
                }else{
                    importNum = importNum + 1;
                }

                for (ListDO listDO : list) {
                    if(StringUtils.isBlank(listDO.getProductName())){
                        continue;
                    }
                    //查询是否已经导入过
                    String existId = listDao.getInfoByProNameAndBatchNum(listDO);
                    if (StringUtils.isNotBlank(existId)) {
                        listDO.setId(existId);
                        listDao.update(listDO);
                    } else {
                        String id = UUID.randomUUID().toString().replaceAll("-", "");
                        listDO.setId(id);
                        listDO.setIsDelete(0);
                        listDO.setCreateUser(ShiroUtils.getUserId().toString());
                        listDO.setPrintNum(0);
                        listDO.setImportNum(importNum);
                        listDO.setWithOrderPrintNum(0);
                        listDao.save(listDO);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导入模板有误", e.getStackTrace());
            throw new Exception("导入模板有误", e);
        }
    }

    @Override
    public DeliveryDetailVO detail(String id) {
        DeliveryDetailVO deliveryDetailVO = listDao.detail(id);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        deliveryDetailVO.setFreightTime(format.format(deliveryDetailVO.getFreightDate()));
        return deliveryDetailVO;
    }

    @Override
    public void updatePrintNum(String[] ids) {
        for(String id : ids){
            ListDO listDO = listDao.get(id);
            listDO.setPrintNum(listDO.getPrintNum() + 1);
            listDao.update(listDO);
        }
    }
}
